﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ITXHWEB.Filters;
using ITXHWEB.Models;
using System.Xml.Linq;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using System.Data;
using ITXHWEB.Models;
namespace ITXHWEB.Controllers
{
    public partial class JLTPDCController : ITXController
    {
        VABPOS ABP;
        VICC_PDC PDC;
        public string date = "";
        public string year = "";
        public string month = "";
        public string day = "";
        string OracleDB = "";
        /*{
            get
            {
                 return app.OracleDB.ConnectionString;
            }
        }*/
        public bool Validate(PDCModel model)
        {
            if (ABP == null)
                ABP = new VABPOS();
            bool IsValid = true;
            if (model.TERMNO.Trim().Length == 0)
            {
                IsValid = false;
            }
            if (model.ABPAUF.Trim().Length < 11)
            {
                IsValid = false;
            }
            else
            {
                DataRow dwData = ABP.SetProd.Tables["ABPOS"].NewRow();
                dwData["ABPMANDANT"] = "00";
                dwData["ABPAUFKRS"] = model.ABPAUF.Substring(0, 2);
                dwData["ABPAUFLFD"] = model.ABPAUF.Substring(2, 6);
                dwData["ABPAUFPOS"] = model.ABPAUF.Substring(8, 3);
                DataSet SResult = ABP.QueryABPOS(dwData, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    if (row["ABPERLKZ"].ToString() == "J")
                    {
                        model.IsDataValid = false;
                        model.REMARK = "订单已经完成";
                    }
                }
            }
            if (model.STAFF.Trim().Length == 0)
            {
                IsValid = false;
            }
            if (model.QMTR == 0 && model.QKG == 0 && model.QYD == 0)
            {
                IsValid = false;
            }
            return IsValid;
        }
        public void Insert(PDCModel model)
        {
            if (PDC == null)
            {
                PDC = new VICC_PDC();
            }
            Helper.GetDate(ref date, ref year, ref month, ref day);
            DataRow dwData = PDC.SetProd.Tables["ICC_PDC"].NewRow();
            PDC.SetUpNewRow(ref dwData);
            dwData["ABAID"] = Guid.NewGuid().ToString();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = model.ABPAUF.Substring(0, 2);
            dwData["ABFAUFLFD"] = model.ABPAUF.Substring(2, 6);
            dwData["ABFAUFPOS"] = model.ABPAUF.Substring(8, 3);
            dwData["QTY"] = model.QMTR;
            dwData["PDCDATE"] = date;
            dwData["PDCSTAFF"] = model.STAFF;
            dwData["NEWTIME"] = date;
            dwData["NEWUSER"] = model.STAFF;
            PDC.InsertICC_PDC(dwData, 1, app.company);
        }
        [AllowAnonymous]
        public ActionResult JLTPDC(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            PDCModel m = new PDCModel();
            if (UserID == "JLTV    ")
            {
                m.TERMNO = app.TERMNO;
                m.ABPAUF = "";
                m.MACHINEID = "";
                m.STAFF = "";
                m.QMTR = 0;
                return PartialView(m);
            }
            return PartialView();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult JLTPDC(PDCModel model, string returnUrl)
        {

            model.TERMNO = app.TERMNO;
            model.STAFF = app.User;
            bool IsDataValid = Validate(model);
            model.IsDataValid = IsDataValid;
            if (IsDataValid)
            {
                Insert(model);
                model.ABPAUF = "";
                model.QMTR = 0;
                model.MACHINEID = "";
                model.STAFF = "";
            }
            else
            { }
           
            return RedirectToAction(MAPP, MAPP);
        }
        //
        // POST: /Account/LogOff

    }
}
